﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Fonlow.CommandLineGui;
using System.Reflection;
using Fonlow.CommandLineGui.Robocopy;

namespace TestBetter
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class UnitTest1
    {
        public UnitTest1()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestGetDefaultValueOfCommandBuilder()
        {
            Options options = new Options();
            options.SlashCopy = CopyFlags.A | CopyFlags.D;


            var optionsBuilderStatic = new PrivateType(typeof(OptionsBuilder));

            PropertyInfo[] propertiesArray = options.GetType().GetProperties();
            foreach (PropertyInfo p in propertiesArray)
            {
                if (p.Name == "SlashCopy")
                {
                    CopyFlags flags = (CopyFlags)optionsBuilderStatic.InvokeStatic("GetDefaultValue", p);
                    Assert.IsTrue(flags == CopyFlags.None);
                    CopyFlags kk = (CopyFlags)p.GetValue(options, null);
                    Assert.IsTrue(options.SlashCopy == kk);
                    return;
                }
            }

            Assert.Fail("Not found.");
        }

        [TestMethod]
        public void TestGetParametersOfCommandBuilder()
        {
            Options options = new Options();
            options.SlashCopy = CopyFlags.A | CopyFlags.D;

            OptionsBuilder b = new OptionsBuilder(options);
            IList<string> parameters = b.DefinedParameters;
            Assert.IsTrue(parameters.Count == 1, "All properties of non strhing type should have a default value.");
            Assert.IsTrue(parameters[0] == "/COPY:DA");

        }

        [TestMethod]
        public void TestRoboCopyCommand()
        {
            RobocopyCommand command = new RobocopyCommand();
            Options options = command.CommandParameters as Options;
            command.Load(
                "robocopy \"\\\\here\\OK here\" \"d\\:\\there is\\A Dir\" file1 \"file 2\" /Z   /DCOPY:T /A-:RASCN /256   /XF abe defg \"c:\\here and there\\kk.doc\" /MAX:23 /Log+:\"c:\\some where\\kk.log\" /CopyAll");
            Assert.IsTrue(options.SlashZ);
            Assert.IsTrue(options.SlashDCopyT);
            Assert.AreEqual((Rashcneto.R | Rashcneto.A | Rashcneto.S | Rashcneto.C | Rashcneto.N), options.SlashAMinus);
            Assert.IsTrue(options.Slash256);
            Assert.AreEqual("abe defg \"c:\\here and there\\kk.doc\"", options.SlashXf);
            Assert.AreEqual(23, options.SlashMax);
            Assert.AreEqual("\"c:\\some where\\kk.log\"", options.SlashLogPlus);
        }
    }
}
